package com.sojson.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sojson.dao.FileDepotDao;
import com.sojson.entity.bo.FileField;
import com.sojson.entity.bo.FileTable;
import com.sojson.entity.po.FileDepot;
import com.sojson.entity.vo.FileDepotVo;
import com.sojson.service.FileDepotService;
import com.sojson.service.impl.thread.FileDepotThreadImpl;
import com.sojson.util.BeanUtil;

/**
 * 文件库Service实现类
 * 
 * @author liu
 * @date 2021-05-26
 */
@Service
public class FileDepotServiceImpl implements FileDepotService {

    @Autowired
    private FileDepotThreadImpl fileDepotThreadImpl;
    @Resource
    private FileDepotDao fileDepotDaoImpl;

    /**
     * 单条插入
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int insert(FileDepot fileDepot) {
        int insert = fileDepotDaoImpl.insert(fileDepot);
        return insert;
    }

    /**
     * 批量插入
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int insertList(FileDepot fileDepot) {
        int insertList = fileDepotDaoImpl.insertList(fileDepot.getImages());
        return insertList;
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int deleteByDto(FileDepot fileDepot) {
        int deleteByDto = fileDepotDaoImpl.deleteByDto(fileDepot);
        return deleteByDto;
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    @Override
    public int countAll() {
        int countAll = fileDepotDaoImpl.countAll();
        return countAll;
    }

    /**
     * 查询所有
     * 
     * @return
     */
    @Override
    public List<FileDepot> listAll() {
        List<FileDepot> findAll = fileDepotDaoImpl.listAll();
        return findAll;
    }

    /**
     * 根据对象统计数量
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int countByDto(FileDepot fileDepot) {
        int countByDto = fileDepotDaoImpl.countByDto(fileDepot);
        return countByDto;
    }

    /**
     * 根据访问路径统计数量
     * 
     * @param url
     * @return
     */
    @Override
    public int countByUrl(String url) {
        return fileDepotDaoImpl.countByUrl(url);
    }

    /**
     * 根据存储路径统计数量
     * 
     * @param path
     * @return
     */
    @Override
    public int countByPath(String path) {
        return fileDepotDaoImpl.countByUrl(path);
    }

    /**
     * 根据对象查询
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public List<FileDepot> listByDto(FileDepot fileDepot) {
        List<FileDepot> findByDto = fileDepotDaoImpl.listByDto(fileDepot);
        return findByDto;
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        int deleteById = fileDepotDaoImpl.deleteById(id);
        return deleteById;
    }

    /**
     * 通过id数组删除(物理删除)
     * 
     * @param ids
     * @return
     */
    @Override
    public int deleteByIds(Integer[] ids) {
        int deleteByIds = fileDepotDaoImpl.deleteByIds(ids);
        return deleteByIds;
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateAllById(FileDepot fileDepot) {
        int updateAllById = fileDepotDaoImpl.updateAllById(fileDepot);
        return updateAllById;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateAllByIdList(FileDepot fileDepot) {
        int updateAllByIdList = fileDepotDaoImpl.updateAllByIdList(fileDepot);
        return updateAllByIdList;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateDtoListAllById(FileDepot fileDepot) {
        int updateDtoListAllById = fileDepotDaoImpl.updateDtoListAllById(fileDepot.getImages());
        return updateDtoListAllById;
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateById(FileDepot fileDepot) {
        int updateById = fileDepotDaoImpl.updateById(fileDepot);
        return updateById;
    }

    /**
     * 通过Url更新
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateByUrl(FileDepot fileDepot) {
        return fileDepotDaoImpl.updateByUrl(fileDepot);
    }

    /**
     * 通过Url更新
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateByUrlArr(FileDepot fileDepot) {
        return fileDepotDaoImpl.updateByUrlArr(fileDepot);
    }

    /**
     * 通过Url更新
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateByUrlList(FileDepot fileDepot) {
        return fileDepotDaoImpl.updateByUrlList(fileDepot);
    }

    /**
     * 通过Path更新
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateByPath(FileDepot fileDepot) {
        return fileDepotDaoImpl.updateByPath(fileDepot);
    }

    /**
     * 通过Path更新
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateByPathArr(FileDepot fileDepot) {
        return fileDepotDaoImpl.updateByPathArr(fileDepot);
    }

    /**
     * 通过Path更新
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateByPathList(FileDepot fileDepot) {
        return fileDepotDaoImpl.updateByPathList(fileDepot);
    }

    /**
     * 通过Url更新状态
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateStatusByUrl(FileDepot fileDepot) {
        return fileDepotDaoImpl.updateStatusByUrl(fileDepot);
    }

    /**
     * 通过Path更新状态
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateStatusByPath(FileDepot fileDepot) {
        return fileDepotDaoImpl.updateStatusByPath(fileDepot);
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateByIdList(FileDepot fileDepot) {
        int updateByIdList = fileDepotDaoImpl.updateByIdList(fileDepot);
        return updateByIdList;
    }

    /**
     * 通过访问路径集合更新状态
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateStatusByUrlArr(FileDepot fileDepot) {
        return fileDepotDaoImpl.updateStatusByUrlArr(fileDepot);
    }

    /**
     * 通过存储路径更新状态
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateStatusByPathArr(FileDepot fileDepot) {
        return fileDepotDaoImpl.updateStatusByPathArr(fileDepot);
    }

    /**
     * 通过访问路径集合更新状态
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateStatusByUrlList(FileDepot fileDepot) {
        return fileDepotDaoImpl.updateStatusByUrlList(fileDepot);
    }

    /**
     * 通过存储路径更新状态
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateStatusByPathList(FileDepot fileDepot) {
        return fileDepotDaoImpl.updateStatusByPathList(fileDepot);
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public int updateDtoListById(FileDepot fileDepot) {
        int updateDtoListById = fileDepotDaoImpl.updateDtoListById(fileDepot.getImages());
        return updateDtoListById;
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @Override
    public FileDepot findById(Integer id) {
        FileDepot findById = fileDepotDaoImpl.findById(id);
        return findById;
    }

    /**
     * 通过id数组查询
     * 
     * @param ids
     * @return
     */
    @Override
    public List<FileDepot> findByIds(Integer[] ids) {
        List<FileDepot> findByIds = fileDepotDaoImpl.findByIds(ids);
        return findByIds;
    }

    /**
     * 通过对象查询id列表
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public List<Integer> findIdByDto(FileDepot fileDepot) {
        List<Integer> findIdByDto = fileDepotDaoImpl.findIdByDto(fileDepot);
        return findIdByDto;
    }

    /**
     * 清空数据(物理清空)
     */
    @Override
    public int clear() {
        return fileDepotDaoImpl.clear();
    }

    /**
     * 根据对象查询存储路径
     * 
     * @param fileDepot
     * @return
     */
    @Override
    public List<FileDepot> listPath(FileDepotVo fileDepot) {
        return fileDepotDaoImpl.listPath(fileDepot);
    }

    /**
     * 获取未关联的文件的存储路径
     * 
     * @param fileTable
     * @param isIncludeOrderRelation    是否包含订单关联文件
     * @return
     */
    @Override
    public List<FileDepot> getPathList(FileTable fileTable, boolean isIncludeOrderRelation) {
        List<FileField> fields = fileTable.getFields();
        List<FileField> arrayList = null;

        if (isIncludeOrderRelation) {
            arrayList = fields;
        } else {
            arrayList = new ArrayList<>();
            for (FileField fileField : fields) {
                if (!fileField.getIsOrderRelation()) {
                    arrayList.add(fileField);
                }
            }
        }

        if (arrayList.size() == 0) {
            return new ArrayList<>(0);
        }
        FileTable ft = new FileTable();
        BeanUtil.copyBeanProp(ft, fileTable);
        ft.setFields(arrayList);
        return fileDepotDaoImpl.getPathList(ft);
    }

    /**
     * 获取未关联的文件的存储路径
     * 
     * @param fileTable
     * @param isIncludeOrderRelation    是否包含订单关联文件
     * @return
     */
    @Override
    public List<FileDepot> getPathListByPath(FileTable fileTable, boolean isIncludeOrderRelation) {
        List<FileField> fields = fileTable.getFields();
        List<FileField> arrayList = null;

        if (isIncludeOrderRelation) {
            arrayList = fields;
        } else {
            arrayList = new ArrayList<>();
            for (FileField fileField : fields) {
                if (!fileField.getIsOrderRelation()) {
                    arrayList.add(fileField);
                }
            }
        }

        if (arrayList.size() == 0) {
            return new ArrayList<>(0);
        }
        FileTable ft = new FileTable();
        BeanUtil.copyBeanProp(ft, fileTable);
        ft.setFields(arrayList);
        return fileDepotDaoImpl.getPathListByPath(ft);
    }

    /**
     * 获取订单关联关系的文件的存储路径
     * 
     * @param fileTable
     * @return
     */
    @Override
    public List<FileDepot> getOrderRelationPathList(FileTable fileTable) {
        List<FileField> fields = fileTable.getFields();
        List<FileField> arrayList = new ArrayList<>();

        for (FileField fileField : fields) {
            if (fileField.getIsOrderRelation()) {
                arrayList.add(fileField);
            }
        }

        if (arrayList.size() == 0) {
            return new ArrayList<>(0);
        }
        FileTable ft = new FileTable();
        BeanUtil.copyBeanProp(ft, fileTable);
        ft.setFields(arrayList);
        return fileDepotDaoImpl.getOrderRelationPathList(ft);
    }

    /**
     * 获取订单关联关系的文件的存储路径
     * 
     * @param fileTable
     * @return
     */
    @Override
    public List<FileDepot> getOrderRelationPathListByPath(FileTable fileTable) {
        List<FileField> fields = fileTable.getFields();
        List<FileField> arrayList = new ArrayList<>();

        for (FileField fileField : fields) {
            if (fileField.getIsOrderRelation()) {
                arrayList.add(fileField);
            }
        }

        if (arrayList.size() == 0) {
            return new ArrayList<>(0);
        }
        FileTable ft = new FileTable();
        BeanUtil.copyBeanProp(ft, fileTable);
        ft.setFields(arrayList);
        return fileDepotDaoImpl.getOrderRelationPathListByPath(ft);
    }

    /**
     * 清空旧文件
     * 
     * @return
     */
    @Override
    public void clearFile() {
        fileDepotThreadImpl.clearFile();
    }

    /**
     * 异常后的警告
     */
    @Override
    public void clearImgMonthWarn() {
        // TODO
    }

}